package chloroform;

import util.StreamUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: 27.2.11
 * Time: 19:09
 * To change this template use File | Settings | File Templates.
 */
public class ExtJSServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String scriptPath = "extjs" + req.getPathInfo();
        InputStream scriptStream = getClass().getClassLoader().getResourceAsStream(scriptPath);
        if (scriptStream == null)
            throw new ServletException("Invalid path to ExtJS resource " + scriptPath + ".");

        String mime = "text/plain";
        if (scriptPath.endsWith(".css")) {
            mime = "text/css";
        } else if (scriptPath.endsWith(".js")) {
            mime = "text/javascript";
        } else if (scriptPath.endsWith(".gif")) {
            mime = "image/gif";
        } else if (scriptPath.endsWith(".png")) {
            mime = "image/png";
        }

        resp.setContentType(mime);

        OutputStream output = resp.getOutputStream();
        StreamUtils.copy(scriptStream, output);
        output.flush();
    }
}
